package com.appbee.gdata.picasa.client.activity;

import javax.inject.Inject;

import com.appbee.core.client.place.HasPlace;
import com.appbee.core.client.place.IndexPlace;
import com.appbee.core.client.place.PlaceControl;
import com.appbee.gdata.picasa.client.PicasaActivityModul;
import com.appbee.gdata.picasa.client.place.PhotoPlace;
import com.appbee.gdata.picasa.client.place.ThumbnailPlace;
import com.appbee.gdata.picasa.shared.SimplePhotoProxy;
import com.google.gwt.activity.shared.AbstractActivity;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.place.shared.Place;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.view.client.HasData;
import com.google.gwt.view.client.Range;
import com.google.gwt.view.client.RangeChangeEvent;

public class PicasaPhotosActivity extends AbstractActivity
implements HasPlace
{

	public interface View extends IsWidget
	{
		public void setActivity(PicasaPhotosActivity activity);
		
		public HasData<SimplePhotoProxy> getHasData();

		public void clearSelection();
	}
	
	private final View view;
	private final PicasaPhotoDataProvider dataProvider;
	private final PlaceControl placeController;

	private ThumbnailPlace place;
	private HandlerRegistration handlerRegistration;
	private final PicasaActivityModul activityModul;
	
	@Inject
	public PicasaPhotosActivity(View view,
								PicasaActivityModul activityModul,
								PicasaPhotoDataProvider dataProvider,
								PlaceControl placeController)
	{
		super();
		this.view = view;
		this.activityModul = activityModul;
		this.dataProvider = dataProvider;
		this.placeController = placeController;
		view.setActivity(this);
	}

	@Override
	public void onStop()
	{
		super.onStop();
		dataProvider.removeDataDisplay(view.getHasData());
		handlerRegistration.removeHandler();
	}

	@Override
	public void start(AcceptsOneWidget panel, EventBus eventBus)
	{
		view.clearSelection();
		int start = Math.max(0,place.getIndex());
		view.getHasData().setVisibleRangeAndClearData(new Range(start, 15),false);
		dataProvider.setAlbumid(place.getAlbumId());
		dataProvider.addDataDisplay(view.getHasData());
		//Keep track of datarange in url history.
		handlerRegistration = this.view.getHasData().addRangeChangeHandler(new RangeChangeEvent.Handler()
		{
			
			@Override
			public void onRangeChange(RangeChangeEvent event)
			{
				placeController.setPlaceStateChange(new ThumbnailPlace(place.getName(),place.getAlbumId(),event.getNewRange().getStart()));
			}
		});

		panel.setWidget(view);
	}

	@Override
	public void setPlace(Place place)
	{
		this.place = (ThumbnailPlace) place;
	}

	public void onPhotoSelected(SimplePhotoProxy selectedObject)
	{
		placeController.goTo(new PhotoPlace(place.getAlbumId(), activityModul
				.getPotoActivityId(),
				selectedObject.getUrl()));
	}

	public void doBack()
	{
		placeController.goTo(new IndexPlace(activityModul.getAlbumActivityId(),
				0));
	}

}
